//
//  MainTabBarViewController.m
//  FurniturePlatform
//
//  Created by beyondSoft on 16/12/22.
//  Copyright © 2016年 beyondSoft. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "UIViewController+TabBarItemImage.h"
#import "BaseUINavigationController.h"
#import "REBaseViewController.h"
//#import "REHomeViewController.h"
//#import "REHistoryViewController.h"
//#import "REMineViewController.h"
//#import "RELoginViewController.h"

#import <UserNotifications/UserNotifications.h>
@interface MainTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic, strong)BaseUINavigationController *homeNav;
@property(nonatomic,strong)BaseUINavigationController *historyNav;
@property (nonatomic, strong)BaseUINavigationController *mineNav;


@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mcTabbar = [[RETabBar alloc] init];
    [self setValue:_mcTabbar forKeyPath:@"tabBar"];
    
    self.delegate = self;

    [self initTabController];
    ObserveNoti(self, @"showTabbar", ShowTabbar);
    ObserveNoti(self, @"hideTabbar", HideTabbar);

 }




- (void)initTabController{


//        _homeNav = [[BaseUINavigationController alloc] initWithRootViewController:[REHomeViewController new]];
//    _mineNav = [[BaseUINavigationController alloc] initWithRootViewController:[REMineViewController new]];
//    _historyNav = [[BaseUINavigationController alloc]initWithRootViewController:[[REHistoryViewController alloc]init]];


    self.viewControllers = @[_homeNav,_historyNav,_mineNav];
    
    _homeNav.tabBarItem.title = REAPPString(@"Home");
    _mineNav.tabBarItem.title = REAPPString(@"Mine");
    _historyNav.tabBarItem.title = REAPPString(@"Star");
    [_historyNav setTabBarIconName:@"tab_icon_order" selectedIconName:@"tab_icon_order_active"];
    [_mineNav setTabBarIconName:@"tab_icon_mine" selectedIconName:@"tab_icon_mineo_active"];
    [_homeNav setTabBarIconName:@"tab_icon_home" selectedIconName:@"home_tab_selected"];
    
    //设置未选中item时的字体
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary
                                                       dictionaryWithObjectsAndKeys:
                                                       colorFromHexString(@"9A9F9B"), NSForegroundColorAttributeName,
                                                       [UIFont boldSystemFontOfSize:12],
                                                       NSFontAttributeName,
                                                       nil] forState:UIControlStateNormal];
    //设置选中item时的字体
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary
                                                       dictionaryWithObjectsAndKeys:
                                                       COLOR_MAIN, NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:12],
                                                       NSFontAttributeName,
                                                       nil] forState:UIControlStateSelected];
 
    self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.translucent = NO;
    self.delegate = self;
    self.hidesBottomBarWhenPushed = YES;
}

- (void)showTabbar{
    self.tabBar.hidden = NO;
    self.tabBar.translucent = NO;
}

- (void)hideTabbar{
    self.tabBar.hidden = YES;
    self.tabBar.translucent = YES;
}
#pragma mark--UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

    if ([viewController.tabBarItem.title isEqualToString:REAPPString(@"Home")]) {
        _mcTabbar.blackView.hidden = YES;
    } else {
        _mcTabbar.blackView.hidden = NO;
    }
    

    return YES;
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
- (UIUserInterfaceStyle)overrideUserInterfaceStyle {
    return UIUserInterfaceStyleLight;
}


- (void)toLogin{
//    if (![GETCURRENTCONTROLLER isKindOfClass:[RELoginViewController class]]) {
//        
//        RELoginViewController *vc = [RELoginViewController new];
//        PUSHCONTROLLER(vc);
//        
//    }
    
}

- (BOOL)shouldAutorotate{
    UINavigationController *vc = self.selectedViewController;
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [[[vc childViewControllers]lastObject]shouldAutorotate];
    } else {
        return [vc shouldAutorotate];
    }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UINavigationController *vc = self.selectedViewController;
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [[[vc childViewControllers]lastObject]preferredInterfaceOrientationForPresentation];
    } else {
        return [vc preferredInterfaceOrientationForPresentation];;
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    UINavigationController *vc = self.selectedViewController;
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [[[vc childViewControllers]lastObject]supportedInterfaceOrientations];
    } else {
        return [vc supportedInterfaceOrientations];
    }
}

@end
